﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace Ftest.medPsw
{
    public partial class frmMain : Form
    {
        CookieContainer gCookie=new CookieContainer();
        static string gsDomain = "202.136.61.13";
        static string gsHomUrl = "http://202.136.61.13:7000";
        static string gsLoginUrl = gsHomUrl + "/Account/Login";
        static string gsPswGenerateUrl = gsHomUrl + "/Home/Generate";
        static string gsRefererUrl = gsHomUrl + "/Home/Anymanage";

        public frmMain()
        {
            InitializeComponent();
        }

        public static string PostLogin(string postData, string requestUrlString, ref CookieContainer cookie)
        {
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] data = encoding.GetBytes(postData);
            //向服务端请求
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            myRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
            myRequest.AllowAutoRedirect = true;
            myRequest.KeepAlive = true;//建立持久性连接
            myRequest.ContentLength = data.Length;
            myRequest.CookieContainer = new CookieContainer();
            Stream newStream = myRequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            //将请求的结果发送给客户端(界面、应用)
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

            string setCookieStr = myResponse.GetResponseHeader("Set-Cookie");
            string cookie1 = myResponse.Headers.Get("Set-Cookie");

            cookie.Add(myResponse.Cookies);
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            return reader.ReadToEnd();
        }
        public static string PostRequest(string postData, string requestUrlString, CookieContainer cookie)
        {
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] data = encoding.GetBytes(postData);
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;
            myRequest.CookieContainer = cookie;
            Stream newStream = myRequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            return reader.ReadToEnd();
        }
        public static string GetRequest(string data, string requestUrlString, CookieContainer cookie)
        {
            System.Net.HttpWebRequest request;
            // 创建一个HTTP请求
            request = (System.Net.HttpWebRequest)WebRequest.Create(requestUrlString + "?" + data);
            request.CookieContainer = cookie;
            //request.Method="get";
            System.Net.HttpWebResponse response;
            response = (System.Net.HttpWebResponse)request.GetResponse();
            System.IO.StreamReader myreader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string responseText = myreader.ReadToEnd();
            myreader.Close();
            //Msg(responseText);
            return responseText;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            gCookie = new CookieContainer();
            gCookie.Add(new Cookie("Medicon.WorkeManage",
                "ECE7E35542F574F079735310D3CBC8ABD606E3ACA98152034783E961C6F03DBE1C26D458DAF1812BAA41A6D63FC0CBE565A0B10D999A8A62CE7B415A5CCD7C793BAA83EC5241F775D481F68076CFDF28C51265FEA1AA61F991565D6F8765B7EBD5263EE71F20767E7530899179B2AD99B3B45D6250E545CFA6886ACBA7CE6701677BECCEA5FF94EACEC30FBA8E6D295BD6595567BCCF30FD3E818D3E00056FBF36B7BAB516DD5416228C1C101B91A63A"
                , "/", gsDomain));
            //gCookie.Add(new Cookie("__RequestVerificationToken",
            //    "Oz3LiOdxnRYbG1-s__SVEqKb5a8zobHJJZPfOm-YRVwQJPfupppnf7FMNj7a7nkfrisSeJkFmwQ_m1j7DYjjQ0Hf3OyQqgk3XByButG-MIs1"
            //    , "/", gsDomain));
            tbMsg.Text = "Begin Login...";
            string data = "__RequestVerificationToken=Oz3LiOdxnRYbG1-s__SVEqKb5a8zobHJJZPfOm-YRVwQJPfupppnf7FMNj7a7nkfrisSeJkFmwQ_m1j7DYjjQ0Hf3OyQqgk3XByButG-MIs1&Username=%E7%8E%8B%E5%B9%BF%E8%B6%85&Password=111&RememberMe=false";
            try
            {
                string r =
                PostLogin(data, gsLoginUrl, ref gCookie);
                if (!r.Contains("反馈信息"))
                    Msg("or err-->Cookie count：" + gCookie.GetCookies(new Uri(gsHomUrl)).Count.ToString());
                //Msg("OK-->" + r);
                //else
                //    Msg("登录失败")
                //for (int i = 0; i < gCookie.GetCookies(new Uri(gsHomUrl)).Count; i++)
                //{
                //    Msg("OK-->Cookie：" + gCookie.GetCookies(new Uri(gsHomUrl))[i].Name + " | " + gCookie.GetCookies(new Uri(gsHomUrl))[i].Value);
                //}
                Msg("ok-->Cookie count：" + gCookie.GetCookies(new Uri(gsHomUrl)).Count.ToString());
                tbUserName.Focus();
                tbUserName.SelectAll();
            }
            catch (Exception ex)
            {
                Msg("Err-->" + ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Msg("开始获取...");
            //for (int i = 0; i < 1000; i++)
            //{
            //    string sCurNum = String.Format("{0:d3}", i);                
            //    string r = GetRequest("userName=" + sCurNum, gsPswGenerateUrl, gCookie);
            //    Msg(sCurNum + " | " + r);
            //}

            try
            {
                string r = GetRequest("userName=" + tbUserName.Text, gsPswGenerateUrl, gCookie);
                Msg("ok->  " + r);
                tbUserName.Focus();
                tbUserName.SelectAll();
            }
            catch (Exception ex)
            {
                Msg("Err-->" + ex.Message);
            }
        }

        private void Msg(string _msg)
        {
            tbMsg.Text += "\r\n" + _msg;
        }

        private void tbUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1.PerformClick();
            }
        }


    }
}
